package com.tedu.springboot2302k.controller;

/*
  用于处理与用户相关的业务操作
 */

import com.tedu.springboot2302k.entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;

@Controller

public class UserController {

    private  static File userDir;
    static {
        userDir=new File("./users");
        if (!userDir.exists()){
            userDir.mkdirs();
        }
    }

    @RequestMapping("/regUser")
    public void reg(HttpServletRequest request, HttpServletResponse response){
        System.out.println("开始处理用户注册...");

        //获取表单信息
        String username=request.getParameter("username");
        String password=request.getParameter("password");
        String nickname=request.getParameter("nickname");
        String ageStr=request.getParameter("age");
        System.out.println(username+","+password+","+nickname+","+ageStr);

        //对浏览器的数据进行必要验证
        if(
                username==null||password==null||nickname==null||ageStr==null||
                        username.isEmpty()||password.isEmpty()||nickname.isEmpty()||ageStr.isEmpty()||
                        !ageStr.matches("[0-9]+")
        ){
            //响应一个出错误页面
            try {
                response.sendRedirect("reg_info_error.html");
            } catch (IOException e) {
                System.out.println("Error input");
            }
            return;
        }

        //验证通过后，就要将该用户的信息保存在服务端
        //转换类型
        int age=Integer.parseInt(ageStr);
        //构造对象
        User user=new User(username,password,nickname,age);
        //放置到目标文件
        File file=new File(userDir,username+".obj");
                //如果用户名存在，就跳转到have_user界面
        if(file.exists()){
            try {
                response.sendRedirect("/have_user.html");
            } catch (IOException e) {
                e.printStackTrace();
            }
            return;
        }
        //建立流
        try(
                FileOutputStream fos=new FileOutputStream(file);
                ObjectOutputStream oos=new ObjectOutputStream(fos);
                ) {

            oos.writeObject(user);
            response.sendRedirect("/reg_success.html");

        } catch (IOException e) {
            e.printStackTrace();
        }

    }
    @RequestMapping("/loginUser")
    public void login(HttpServletRequest request, HttpServletResponse response){
        System.out.println("开始处理用户登录...");
        //获取表单信息：
        String usernameGet=request.getParameter("username");
        String passwordGet=request.getParameter("password");
        //判断是否是空
        if(usernameGet.isEmpty()||passwordGet.isEmpty()){
            try {
                response.sendRedirect("login_info_error.html");
            } catch (IOException e) {
                e.printStackTrace();
            }
            return;
        }

//        读取users目录下对应的用户文件(反序列化)
        User archive=null;
        File file =new File(userDir,usernameGet+".obj");
        if(!file.exists()){
            try {
                response.sendRedirect("login_info_error.html");
            } catch (IOException e) {
                e.printStackTrace();
            }
            return;
        }
        try (FileInputStream fis=new FileInputStream(file);
             ObjectInputStream ois=new ObjectInputStream(fis)){
            archive=(User)ois.readObject();
            System.out.println(archive);
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        }

//        比对用户密码
        if(archive.getPassword().equals(passwordGet)){
            //登录成功
            try {
                response.sendRedirect("login_success.html");
            } catch (IOException e) {
                System.out.println("登录成功");
            }
            return;
        }else{

                try {
                    response.sendRedirect("login_fail.html");
                } catch (IOException e) {
                    e.printStackTrace();
                }

            //登录失败
            return;
        }

    }
}
